package com.awenhui.demo.dao;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.HashMap;
import java.util.List;

/**
 * @author yuxf
 * @version 1.0
 * @date 2020/5/21 9:25
 */

public interface MetaDataMapper {

    @SelectProvider(type = MetaDataProvider.class, method = "getTablesSql")
    List<HashMap<String, String>> getTables(String schema);

    @Select("select SCHEMA_NAME from information_schema.schemata")
    List<HashMap<String, String>> getSchemas();

    @Select("${sql}")
    List<HashMap<String, String>> getRows(@Param("sql") String sql);

    /**
     * 元数据sql提供类
     */
    class MetaDataProvider {
        /**
         * 查询表
         *
         * @param schema 数据库名称
         * @return
         */
        public String getTablesSql(String schema) {
            String sql = "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_TYPE='BASE TABLE'";
            if (StringUtils.isBlank(schema)) {
                sql += " AND TABLE_SCHEMA=(select database())";
            } else {
                sql += " AND TABLE_SCHEMA='" + schema + "'";
            }
            return sql;
        }

    }
}


